package comScript

import (
	"encoding/json"
	"fmt"
)

type ChildrenOne struct {
	Text     string        `json:"text"`
	Type     string        `json:"type"`
	Children []ChildrenOne `json:"children"`
}

func GetString(text string) string {
	var (
		str string
	)
	var result []ChildrenOne
	// 将JSON字符串解码到map
	err := json.Unmarshal([]byte(text), &result)
	if err != nil {
		fmt.Println(err)
	}
	str = GetParentStr(result)
	return str
}

func GetParentStr(result []ChildrenOne) string {
	var str string
	for _, item := range result {
		if item.Text != "" {
			str = item.Text
		}
		str += GetChildrenStr(item.Children)
		if item.Text == "" {
			str += "\n"
		}

	}
	return str
}
func GetChildrenStr(item []ChildrenOne) string {
	var str string
	for _, itemData := range item {
		str += itemData.Text
		if len(itemData.Children) > 0 {
			str += GetParentStr(itemData.Children)
		}
	}
	return str
}
